<?php
defined('_JEXEC') or die('Restricted access');

/**
 * Description of eventManager
 *
 * @todo 
 * 
 * @author George Zeakis<zeageorge@google.com>
 * 
 */
class eventManager {
    /**
     *
     * @var eventManager
     */
    protected static $_instance = NULL;
    
    /**
     *
     * @var pfp
     */
    protected $_parent = NULL;
    
    /**
     *
     * @var array
     */
    protected $_registered_events = array();
    
    /**
     *
     * @var array
     */
    protected $_registered_actions = array();

    /**
     * 
     * @param pfp $parent
     * @return eventManager
     */
    public static function Init(pfp $parent){
        if( self::$_instance === NULL ) { 
            self::$_instance = new self();
            self::$_instance->_parent = $parent;
            self::$_instance->_registered_events = array();
            self::$_instance->_registered_actions = array();
        }
        
        return self::$_instance;
    }
    
    public static function registerEvents(array $events){
        
    }
    
    public static function registerEvent($eventName){
        
    }
    
    public static function removeEvent($eventName){
        
    }
    
    public static function removeEvents(array $events){
        
    }
    
    public static function getEvents(){
        
    }

    /**
     * 
     * @param mix &$eventData
     * @return boolean
     */
    public static function riseEvent(&$eventData){
        
        if (version_compare(PHP_VERSION, '5.4.0') >= 0){ $dt=debug_backtrace(0,2);} else { $dt=debug_backtrace(FALSE); }
        
        // todo: record the error.
        if (count($dt)<2){ return FALSE; }
        
        //echo '<pre>';
        //var_dump($dt[0]['file']);     // file of the caller
        //var_dump($dt[1]['class']);    // class of the caller
        //var_dump($dt[1]['function']); // function of the caller
        //echo "<br />\n";
        //echo "from eventManager:<br />\n";
        //var_dump($dt[0]['args']);     // riseEvents args ByRef !!!!
        //var_dump($dt[0]['args'][0]['args']);
        
        $event_data =&$eventData;
        
        if (!is_array($event_data)){ return FALSE; } 
        if (!array_key_exists("event_name", $event_data)){ return FALSE;}
        if ($event_data["event_name"]=="event_onError"){
            echo "<pre>";
            $data2Display = "class: ".$dt[1]['class'].", ";
            $data2Display .= "function: ".$dt[1]['function'].", ";
            $data2Display .= "event: ".$event_data["event_name"].", ";
            $data2Display .= "<br />\n";

            echo $data2Display;
            var_dump($event_data['args']);
        }
        // todo: here check if $event_data["event_name"] is registered from caller eg. already exists in $this->_registered_events
        // if not, just return FALSE;
        
        if (array_key_exists("args", $event_data)){
            // here we have 'args', call (run) all registered actions (for this event) providing them the 'args'
        } else {
            // here we don't have 'args', just call (run) all registered actions (for this event)
        }
        return TRUE;
    }
    
    protected static function changeValues($array){
        // test function for EventActions prototype.
        return $arr;
    }


    public static function getEvents_ofObject($object){
        
    }

    public static function getEvent_Actions($eventName){
        
    }

    public static function getSuspendedEvents(){
        
    }
    
    public static function getSuspended_Actions($of_eventName){
        
    }
    
    public static function suspendEvent($eventName){
        //for how long ???
    }
    
    public static function suspendAction($of_eventName){
        //for how long ???
    }
    
    public static function removeEventActions(array $actions){
        
    }
    
    public static function removeEventAction($action){
        
    }
    
    public static function registerEventAction($event, $action){
        
    }
    
    public static function registerAction_forAllEvents_ofObject($action, $object){
        
    }
    
    public static function removeAllEventActions($event){
        
    }
    
    public static function getNextRunningAction($of_event){
        
    }
    
    public static function getPreviousRunningAction($of_event){
        
    }
    
    public static function getRunningEvent(){
        
    }
    
    /* 
     * we don't permit an explicit call of the constructor! 
     * (like $v = new Singleton()) 
     * 
     */
    protected function __construct(){}
 
    /* 
     * we don't permit cloning the singleton (like $x = clone $v) 
     * 
     */
    protected function __clone(){}
 
}

?>
